package com.woniu.core.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.core.entity.CoreRecourse;
import com.woniu.core.entity.CoreUser;
import com.woniu.core.entity.vo.CoreRecourseVO;
import com.woniu.core.mapper.CoreRecourseMapper;
import com.woniu.core.mapper.CoreUserMapper;
import com.woniu.core.service.CoreRecourseService;
import com.woniu.finance.entity.Asset;
import com.woniu.finance.entity.Finance;
import com.woniu.finance.mapper.AssetMapper;
import com.woniu.finance.mapper.FinanceMapper;
import com.woniu.util.utils.HttpResult;
import com.woniu.util.utils.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CoreRecourseServiceImpl implements CoreRecourseService {

    @Autowired(required = false)
    private CoreRecourseMapper recourseMapper;

    @Autowired(required = false)
    private AssetMapper assetMapper;

    @Autowired(required = false)
    private FinanceMapper financeMapper;

    @Autowired(required = false)
    private CoreUserMapper coreUserMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询所有待追偿的项目
     */
    @Override
    public HttpResult<?> pendingRecourse(String coreUserAccount) {
        QueryWrapper<CoreUser> userQuery = new QueryWrapper<>();
        userQuery.eq("account", coreUserAccount);
        CoreUser user = coreUserMapper.selectOne(userQuery);
        QueryWrapper<CoreRecourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("recourse_time")
                .eq("core_id", user.getUserCoreId());
        List<CoreRecourse> recourseList = recourseMapper.selectList(queryWrapper);
        return new HttpResult<>().ok(recourseList);
    }

    /**
     * 查询所有代偿记录
     */
    @Override
    public HttpResult<?> checkRecourse(RequestParam<String> requestParam) {
        QueryWrapper<CoreRecourse> queryWrapper = new QueryWrapper<>();
        String aName = requestParam.getData();
        String coreId = coreUserMapper.selectUserCoreIdByUserAccount(aName);
        queryWrapper.isNotNull("recourse_time")
                .orderByDesc("recourse_time")
                .eq("core_id", coreId);
        PageHelper.startPage(requestParam.getPageNum(), requestParam.getPageSize());
        List<CoreRecourse> recourseList = recourseMapper.selectList(queryWrapper);
        PageInfo pageInfo = new PageInfo(recourseList);
        List<CoreRecourseVO> coreRecourseVO = new ArrayList<>();
        for (CoreRecourse recourse : recourseList) {
            String fId = assetMapper.selectById(recourse.getAssetsId()).getOwnedEnterprise();
            String financeName = financeMapper.selectById(fId).getEnterpriseName();
            CoreRecourseVO vo = new CoreRecourseVO(financeName, recourse.getAssetsTrueValue(), recourse.getCompensationValue(), recourse.getRemainingAmount(), recourse.getRecourseTime());
            coreRecourseVO.add(vo);
        }
        pageInfo.setList(coreRecourseVO);
        return new HttpResult<>().ok(pageInfo);
    }

    /**
     * 代偿
     */
    @Override
    public HttpResult<?> doRecourse(Integer recourseId) {
        CoreRecourse recourse = recourseMapper.selectById(recourseId);
        Asset asset = assetMapper.selectById(recourse.getAssetsId());
        Finance finance = financeMapper.selectById(asset.getOwnedEnterprise());
        finance.setBusinessCapital(finance.getBusinessCapital().add(recourse.getRemainingAmount()));
        int fResult = financeMapper.updateById(finance);
        recourse.setRecourseTime(Timestamp.valueOf(sdf.format(new Date())));
        int rResult = recourseMapper.updateById(recourse);
        if (rResult != 0 && fResult != 0) {
            return new HttpResult<>().ok();
        } else {
            return new HttpResult<>().fail();
        }
    }
}
